package bigbeard.tools.net.server.impl;


import bigbeard.tools.net.server.BaseConnector;
import org.java_websocket.WebSocket;
/**
 * @author bigbeard on 2022-09-29
 */
public class WebsocketConnectorImpl extends BaseConnector {
    private WebSocket conn;

    @Override
    public int send(String message) throws Exception {
        if (null != this.conn && this.conn.isOpen()) {
            this.conn.send(message);
            return message.length();
        }
        return SEND_ERROR;
    }

    @Override
    public int send(byte[] message) throws Exception {
        if (null != this.conn && this.conn.isOpen()) {
            this.conn.send(message);
            return message.length;
        }
        return SEND_ERROR;
    }


    @Override
    public void setConnector(Object client) {
        if (client instanceof WebSocket) {
            this.conn = (WebSocket) client;
        }
    }

    @Override
    public void close() {
        if (null != this.conn) {
            this.conn.close(1000);
        }
        this.conn = null;
    }
}
